/**
 * 房間中籌碼視圖控制
 * 
 * @author LuoDong
 * @email luo4521@online.net.tw
 * 2010-3-8 15:57
 */
package com.addcn.texas.view
{
	import com.addcn.texas.utils.Pot;
	import flash.display.*;
	import flash.events.Event;	
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	import com.addcn.fog.view.*;
	import com.addcn.fog.control.*;
	import com.addcn.fog.model.*;		
	import com.addcn.texas.model.*;
	import com.addcn.texas.control.*;
	import com.addcn.texas.view.components.*;	
	import com.addcn.texas.utils.Coordinate;
	import com.addcn.texas.utils.Chips;
	import com.addcn.common.utils.AppLibary;
	import com.addcn.common.utils.RemoveAllChildren;
	import com.addcn.common.event.ParamEvent;
	
	import caurina.transitions.Tweener;
	
	public class  ChipView  extends View 
	{		
		public static const MOVE_CHIPS_COMPLETE : String = "moveChipsComplete";
		
		private var _container : Object;
		private var _app : AppLibary;		
		private var _bet_class : Class;
		private var _chip_pos : Array;
		private var _pool_pos : Array;
		private var _sit_pos : Array;
		
		private var _bet_obj : Object;   //下注对象
		private var _pot_arr : Array;		//池底对象数组
		private var _game_over : Boolean = false; 
		private var _pot_data : Array; //池底数据
		private var _result_data : Array;
		private var _has_bet : Boolean;
		private var _lock : Boolean;
		
		
		public function ChipView(r_target : Object) : void
		{
			//构建容器	
			this._container = new Sprite;	
			r_target.addChild(this._container);					
			_initialize();
		}
		
		private function _initialize() : void
		{			
			this._app = AppLibary.getInstance();	
			this._bet_class = _app.getClass("com.addcn.texas.view.components.BetPanel");			
			this._chip_pos = Coordinate.getInstance().ChipCoord();
			this._pool_pos = Coordinate.getInstance().PoolCoord();
			this._sit_pos = Coordinate.getInstance().SitCoord();
			
			_bet_obj = {};
			_pot_arr = [];
			this._result_data = [];
			_has_bet = false;
			this._lock = false;
			
		}
		
		override public function notificationsList():void 
		{
			notifications = [							
							GameInfoModel.GAME_START,														
							GameInfoModel.USER_BET,						
							GameInfoModel.POOL_LIST,
							GameInfoModel.GAME_OVER,
							GameInfoModel.LOSE_PLAYER,
							GameInfoModel.REMAIN_PLAYER,
							PokerView.ALLOT_CHIPS,
							];
		}
		
		override public function onDataChanged(r_notification:String, r_data:Object = null):void {
			switch (r_notification) {			
				case GameInfoModel.GAME_START:
					_bet_obj = { };
					
					for (var k in _pot_arr) {
						if(_pot_arr[k].parent){
							_pot_arr[k].parent.removeChild(_pot_arr[k]);
						}
						delete _pot_arr[k];
					}
					//_pot_arr = [];
					this._result_data = [];
					break;										
				case GameInfoModel.USER_BET:   //下注						   
					_showBetChip(r_data);					
					break;
				case GameInfoModel.POOL_LIST:  
					this._pot_data = r_data as Array;  //池底数组 [int,int,..]
					if (this._has_bet) {						
						_amassChips(); //收集筹码，生成池底
					}else if(this._result_data.length >0) {
						_dividePot();  //分配筹码
					}
					break;
				case GameInfoModel.GAME_OVER:  //游戲結束,分配池底		
					this._result_data = r_data as Array;													
					break;
				case GameInfoModel.LOSE_PLAYER:					

					break;                
				case GameInfoModel.REMAIN_PLAYER:								
					_remainOnePlayer(r_data as Array);
					break;
				case PokerView.ALLOT_CHIPS:

					break;
			
			}
		}         
			
		/**
		 * 顯示玩家下注的籌碼
		 * @param	r_data Object
		 */
		private function _showBetChip(r_data : Object) : void
		{			
			_has_bet = true;
			var _sit : int = r_data["sit"];
			var _chip : int = r_data["bet"];
			var _chip_obj : Chips = this._bet_obj[_sit];			
			if (_chip > 0) {				
				if (!_chip_obj) {
					_chip_obj = new Chips(_chip);
					_chip_obj.setPosition(_chip_pos[_sit]["x"], _chip_pos[_sit]["y"]);
					_chip_obj.show(this._container);
					this._bet_obj[_sit] = _chip_obj;
				}else {					
					_chip_obj.modifyChip(_chip);
				}				
			}
		}
						
		/**
		 * 將玩家下注的籌碼集中
		 * 
		 */
		private function _amassChips() : void
		{			
			var _tx : int = this._pool_pos[4]["x"];
			var _ty : int = this._pool_pos[4]["y"];
			var _index : int = this._pot_arr.length -1;
			if (_pot_arr[_index]) {
				_tx = _pot_arr[_index].x;
				_ty = _pot_arr[_index].y;				
			}			
			var _obj : Chips;				
			var _delay : int = 0;		
			var _num : int = 0;
			var _caller : Function = function(evt : Event) : void
			{
				_num--;
				if (_num == 0) {
					_createPot(_delay);
					return;
				}
			}
			for (var k in _bet_obj) {
				_num++;
			}			
			for ( k in _bet_obj) {				
				//if(_delay == 0){
					//_delay = 500;
				//}
				_obj = _bet_obj[k];
			 	_obj.addEventListener(Chips.MOVE_EVENT, _caller);
				_obj.move(_tx, _ty);					
				delete _bet_obj[k];
			}	
			
			if (_num == 0) {
				_createPot(_delay);
			}				
			
			return;
			
			
			if (_delay == 0 ) {
				
			}else {
				var _timer : Timer = new Timer(_delay, 1);
				var _caller : Function = function(evt: Event) : void
				{
					_timer.stop();
					_timer.removeEventListener(TimerEvent.TIMER, _caller);
					_createPot(_delay);
				}				
				_timer.addEventListener(TimerEvent.TIMER, _caller);
				_timer.start();
			}
		}		
					
		/**
		 * 生成池底
		 * @param	r_data Array [int,int,...]
		 */
		private function _createPot(r_delay:int ) : void
		{				
			var _pot : Array = this._pot_data;
			var _obj : Chips;			
			var _val : int;
			var _len : int = _pot_arr.length;
			var _index : int = ((9 - _len) * 0.5 ) >> 0;			
			var _tx : int;
			var _ty : int;			
			for (var _k in _pot)
			{
				var _data : Object =  _pot[_k];		
				_val = _pot[_k];
				_obj = _pot_arr[_k];
				if (!_obj) {
					_obj = new Chips(_val);	
				}else {
					if(_obj.amount != _val){
						_obj.modifyChip(_val);
					}
				}
				_pot_arr[_k] = _obj;
				_tx = this._pool_pos[_index]["x"];
				_ty = this._pool_pos[_index]["y"];
				_obj.setPosition(_tx, _ty);
				_obj.show(this._container);
				//_obj.addEventListener(Chips.MOVE_EVENT, _moveComplete);
				_index++;				
			}
			
			//分配池底				
			var _timer : Timer = new Timer(2000, 1);
			var _caller : Function = function(evt: Event) : void
			{
				_dividePot();
			}
			_timer.addEventListener(TimerEvent.TIMER, _caller);
			_timer.start();	
		}
				
		/**
		 * 劃分獎池
		 */
		private function _dividePot() : void
		{			
			if (this._result_data.length <= 0) return;  //是否有游戏结果
			if (this._lock == true) return; 
			this._lock = true; //加锁，避免重复执行			
			MonsterDebugger.trace("分配筹码", this._result_data);			
			var _res : Array = this._result_data.concat();
			this._result_data = [];
			var _pot : Array = this._pot_data;
			var _x : int;
			var _y : int;
			var _index : Number  = _pot.length -1 ;			
			var _caller : Function = function(evt : Event) : void
			{
				_updateChipd(_index);
				_index--;				
				_handler();
			}
			var _handler : Function = function() : void
			{
				if (_index < 0) return;
				for (var j in _res[_index])
				{
					var _winer : Object = _res[_index][j];
					var _pot_obj : Chips = _pot_arr[_index].cut(_winer["chip"]);
					_x = _sit_pos[_winer["sit"]]["x"];
					_y = _sit_pos[_winer["sit"]]["y"];
					_pot_obj.move(_x, _y);
					_pot_obj.addEventListener(Chips.MOVE_EVENT, _caller);
				}
			}
			_handler();					
			this._lock = false;  //解锁
		}
		
		//其他玩家棄牌,剩余最后一個玩家,獲得籌碼
		private function _remainOnePlayer(r_data: Array) : void
		{
			var _data : Object = r_data[0][0];
			var _pot : Chips;
			var _sit : int = _data["sit"];			
			var _x : int = _sit_pos[_sit]["x"];
			var _y : int = _sit_pos[_sit]["y"];
			var _bool : Boolean = true;
			for each( _pot in _pot_arr) {
				_pot.move(_x, _y);				
				if(_bool){
					_pot.addEventListener(Chips.MOVE_EVENT, _moveComplet);
				}
				_bool = false;
			}			
		}
		
		private function _moveComplet(evt : Event) : void
		{
			evt.target.removeEventListener(Chips.MOVE_EVENT, _moveComplet);
			_updateChipd(0);
		}
		
		private function _updateChipd(r_index : int) : void
		{
			MonsterDebugger.trace("UPDATACHIP", r_index);
			var _model : GameInfoModel = ModelLocator.getInstance().getModel(GameInfoModel);
			_model._updateChip(r_index);			
		}
		
		
		private function _distributeChip(evt : TimerEvent) : void
		{			
			
		}
		
		private function _losePlayer() : void
		{
			
		}
				
		override public function destroy():void 
		{
			RemoveAllChildren.remove(this._container as DisplayObject);
		}
		
	}
	
}
